<?php
$joinToSpaceStr = Yii::t('site', 'Join to Space');
$spaceStr = Yii::t('site', 'space');
$youCanFollowUpStr = Yii::t('site', 'You can follow up');
$joinToRoleStr = Yii::t('site', 'Join to role');
$joinAsCustomerStr = Yii::t('site', 'Join as customer');
$joinAsMemberStr = Yii::t('site', 'Join as member');
$joinAsAdministratorStr = Yii::t('site', 'Join as administrator');
$needApproveStr = Yii::t('site', 'need approve');
$closeStr = Yii::t('site', 'Close');
$joinToSpaceOfStr = Yii::t('site', 'Join to space of');
$asMemberOfStr = Yii::t('site', 'as member of');
$saveChangesStr = Yii::t('site', 'Save changes');
?>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel"><?php echo $joinToSpaceStr; ?></h3>
</div>
<div class="modal-body">
    <?php
    $model = Blyn::app()->getActiveApp()->getFormModel('JoinSpaceFormModel');
    ?>

    <?php
    $space = new BSpace($spaceId);
    $apps = $space->getApps();
    $screen = Blyn::app()->getUIManager()->getCurrentScreen();


    $allowJoinRoles = $space->getSpaceEngine()->getAllowJoinSpaceRoles();
//$applyJoinOperation = $space->getSpaceEngine()->getOperation('applyJoinSpace');
    $customerRoles = array();
    if (isset($allowJoinRoles['customerRoles'])) {
        $customerRoles[$spaceId] = $allowJoinRoles['customerRoles'];
    }
    $autoJoinRoles = array();
    if (isset($allowJoinRoles['autoJoinRoles']))
        $autoJoinRoles[$spaceId] = $allowJoinRoles['autoJoinRoles'];
    $memberRoles = array();
    if (isset($allowJoinRoles['memberRoles']))
        $memberRoles[$spaceId] = $allowJoinRoles['memberRoles'];
    $adminRoles = array();
    if (isset($allowJoinRoles['adminRoles']))
        $adminRoles[$spaceId] = $allowJoinRoles['adminRoles'];

    $autoJoinedId = NULL;

    if ($screen->isAjaxLinkClicked(array('ajaxLinkId' => 'link' . $spaceId))) {
        if (isset($_POST['optionsRadios'])) {
            $checkedRoleId = $_POST['optionsRadios'];
        }

        $spaceAutoJoinRoles = $autoJoinRoles[$spaceId];

        foreach ($spaceAutoJoinRoles as $id => $role) {
            if ($checkedRoleId == $id) {//auto join for autoJoinRoles
                Blyn::app()->getCurrentUser()->addRole($checkedRoleId);
                $autoJoinedId = $id;
            } else {
                $operation = Blyn::app()->getActiveOperation();
                //$operation->getMessenger('requestJoinMessage')->sendMessage(new BMessage());
            }
        }
    }
    ?>

    <legend><?php echo $spaceStr; ?>:<strong><?php echo $space->getName(BEntity::Name_DisplayName); ?></strong></legend>
    <?php
    $spaceAutoRoles = isset($autoJoinRoles[$spaceId]) ? $autoJoinRoles[$spaceId] : array();
    if ($spaceAutoRoles != NULL) {
        ?> 
        <p><strong><?php echo $youCanFollowUpStr; ?>:</strong></p>
    <?php } ?>
    <?php
    foreach ($spaceAutoRoles as $id => $role) {
        $app = $apps[$role->appId];
        ?>
        <label class="radio">
            <?php
            if ($autoJoinedId != NULL && $autoJoinedId == $id)
                echo CHtml::activeRadioButton($model, 'roleId', array('id' => 'ration_' . $id, 'name' => "optionsRadios", 'value' => $id, 'uncheckValue' => null, 'disabled' => TRUE, 'class' => 'muted'));
            else {
                echo CHtml::activeRadioButton($model, 'roleId', array('id' => 'ration_' . $id, 'name' => "optionsRadios", 'value' => $id, 'uncheckValue' => null));
            }
            ?>    
            <?php if ($autoJoinedId != NULL && $autoJoinedId == $id) { ?>
                <div class="muted"> 
                <?php } ?>
                <?php echo $joinToRoleStr; ?>:
                <strong><?php echo $space->getName(BEntity::Name_DisplayName); ?></strong>||
                <strong><?php echo Yii::t('site', $app->getName()); ?></strong>||
                <strong> <?php echo Yii::t('site', $role->getName()); ?></strong>
                <?php if ($autoJoinedId != NULL && $autoJoinedId == $id) { ?>
                </div> 
            <?php } ?>
        </label>
    <?php } ?>
    <?php
    $spaceCustomerRoles = isset($customerRoles[$spaceId]) ? $customerRoles[$spaceId] : array();
    if ($spaceCustomerRoles != NULL) {
        ?>
        <p><strong><?php echo $joinAsCustomerStr; ?>:<span class="text-warning">[<?php echo $needApproveStr; ?>]</span></strong></p>
    <?php } ?>
    <?php
    foreach ($spaceCustomerRoles as $id => $role) {
        $app = $apps[$role->appId];
        ?>          
        <label class="radio">
            <?php
            echo CHtml::activeRadioButton($model, 'roleId', array('id' => 'radio_' . $id, 'name' => "optionsRadios", 'value' => $id, 'uncheckValue' => null));
            ?>
            <?php echo $joinToRoleStr; ?>:
            <strong><?php echo $space->getName(BEntity::Name_DisplayName); ?></strong>||
            <strong><?php echo Yii::t('site', $app->getName()); ?></strong>||
            <strong> <?php echo Yii::t('site', $role->getName()); ?></strong>       
        </label>
    <?php } ?>
    <?php
    $spaceMemberRoles = isset($memberRoles[$spaceId]) ? $memberRoles[$spaceId] : array();
    if ($spaceMemberRoles != NULL) {
        ?>
        <p><strong><?php echo $joinAsMemberStr; ?><span class='text-warning'>[<?php echo $needApproveStr; ?>]</span>:</strong></p>
    <?php } ?>
    <?php
    //$spaceMemberRoles = isset($memberRoles[$spaceId]) ? $memberRoles[$spaceId] : array();
    foreach ($spaceMemberRoles as $id => $role) {
        $app = $apps[$role->appId];
        ?>
        <label class="radio">
            <?php
            echo CHtml::activeRadioButton($model, 'roleId', array('id' => 'radion_' . $id, 'name' => "optionsRadios", 'value' => $id, 'uncheckValue' => null));
            ?>
            <?php echo $joinToRoleStr; ?>:
            <strong><?php echo $space->getName(BEntity::Name_DisplayName); ?></strong>||
            <strong><?php echo Yii::t('site', $app->getName()); ?></strong>||
            <strong> <?php echo Yii::t('site', $role->getName()); ?></strong>       
        </label>
    <?php } ?>
    <?php
    $spaceAdminRoles = isset($adminRoles[$spaceId]) ? $adminRoles[$spaceId] : array();
    if ($spaceAdminRoles != NULL) {
        ?>
        <p><strong><?php echo $joinAsAdministratorStr; ?>:[<?php echo $needApproveStr; ?>]</strong></p>
    <?php } ?>
    <?php
    //$spaceAdminRoles = isset($adminRoles[$spaceId]) ? $adminRoles[$spaceId] : array();
    foreach ($spaceAdminRoles as $id => $role) {
        ?>
        <label class="radio">
            <input type="radio" name="optionsRadios" id="optionsRadios<?php echo $id; ?>" value="<?php echo $id; ?>" checked>
            <?php echo $joinToSpaceOfStr; ?> <strong><?php echo $space->getName(BEntity::Name_DisplayName); ?></strong> <?php echo $asMemberOfStr; ?> <strong> <?php echo $role->getName(); ?> </strong>        
        </label>
    <?php } ?>

</div>

<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo $closeStr; ?></button>
    <?php
    echo $screen->getAjaxLink($saveChangesStr, array(
        'ajaxLinkId' => 'link' . $spaceId,
        'ajaxContainerId' => 'myModal' . $spaceId,
        'ajaxView' => array('applyJoinSpace', 'data' => array('spaceId' => $space->getId())),
        'htmlOptions' => array('class' => 'btn btn-primary')));
    ?>
</div>


